var epsg3857 = require("./epsg3857");
// var epsg4326 = require("./epsg4326");
var projtool = require('./fc-projtool.min.js').proj;

function OsmSource(){

    this.projection = 'EPSG:3857';

    this.origin = [-20037508.3427892, 20037508.3430388];

    this.defaultUrl = 'http://webst01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}';

    this.resolutions = epsg3857.resolutions;

    this.tileUrlFunction = function(z,x,y){
        y = -1 - y;
        // 瓦片地址模板
        var url = this.defaultUrl;
        url = url.replace('{x}', x.toString()).replace('{y}', y.toString()).replace('{z}', z.toString());
        return url;
    };

    this.requestSetting = {
        host: 'webst01.is.autonavi.com',
        port:'80',
        method:'GET',
        headers: {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.8',
            'Cache-Control': 'max-age=0',
            'Connection': 'keep-alive',
            'Host': 'webst01.is.autonavi.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36',
            'Upgrade-Insecure-Requests':1
        }// 伪造请求头
    };

    /**
     * extent [xmin, ymin, xmax, ymax]
     */
    this.calculateXYRange = function(extent, baseSize){
        var baseSize = baseSize?baseSize:10; // 单位KB
        var self = this;
        let lefttop = [extent[0], extent[3]];
        let rightbottom = [extent[2], extent[1]];
        let lefttop_ = projtool.transform(lefttop, 'EPSG:4326', self.projection);
        let rightbottom_ = projtool.transform(rightbottom, 'EPSG:4326', self.projection);

        let XYZRange = [];
        for(var i=0; i<self.resolutions.length; i++){
            let startTile = self.calculateXY([lefttop_[0], lefttop_[1], i]);
            let endTile = self.calculateXY([rightbottom_[0], rightbottom_[1], i]);
            XYZRange[i] = {
                level: i,
                rowStart: startTile.row,
                rowEnd: endTile.row,
                colStart: startTile.col,
                colEnd: endTile.col,
                count: Math.abs((endTile.row - startTile.row +1 )) * Math.abs((endTile.col - startTile.col +1))
            };
            XYZRange[i].size = baseSize * XYZRange[i].count;
            if(XYZRange[i].size<1024){
                XYZRange[i].sizeStr = (XYZRange[i].size).toFixed(2) + 'KB';
            }
            else if(XYZRange[i].size>=1024 && XYZRange[i].size<1048576){
                XYZRange[i].sizeStr = (XYZRange[i].size/1024).toFixed(2) + 'MB';
            }else{
                XYZRange[i].sizeStr = (XYZRange[i].size/(1024*1024)).toFixed(2) + 'GB';
            }
        }

        return XYZRange;
            
    };

    this.calculateXY = function(coord){
        var self = this;
        var resolution = self.resolutions[coord[2]];
        var row = Math.floor((coord[0] - self.origin[0]) / (256*resolution));
        var col;
        var _col = (coord[1] - self.origin[1]) / (256*resolution);
        if(_col<0){
            var col = Math.floor(_col);	
        }
        else{
            var col = Math.floor(_col);
            col = col + 1;
        }
        return {
            row: row,
            col: -1 - col
        };
    };

};

module.exports = OsmSource;